package monster.com.cvh.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

import monster.com.cvh.R;

/**
 * Created by ngfngf on 2017/1/31.
 */

public class HorizontalProgressBarwithProgress extends ProgressBar {
    private static final int DEFAULT_TEXT_SIZE = 18;//sp
    private static final int DEFAULT_TEXT_COLOR = Color.parseColor("#31eb91");
    private static final int DEFAULT_COLOR_UNREACH = Color.parseColor("#1c8ac3");
    private static final int DEFAULT_HEIGHT_UNREACH = 2;//dp
    private static final int DEFAULT_COLOR_REACH = Color.parseColor("#51eb9d");
    private static final int DEFAULT_HEIGHT_REACH = 2;//dp
    private static final int DEFAULT_TEXT_OFFSET = 10;//dp

    protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);
    protected int mTextColor = DEFAULT_TEXT_COLOR;
    protected int mUnreachColor = DEFAULT_COLOR_UNREACH;
    protected int mUnreachHeight = dp2px(DEFAULT_HEIGHT_UNREACH);
    protected int mReachColor = DEFAULT_COLOR_REACH;
    protected int mReachHeight = dp2px(DEFAULT_HEIGHT_REACH);
    protected int mTextOffset = dp2px(DEFAULT_TEXT_OFFSET);

    protected Paint mPaint = new Paint();
    private int RealWidth;

    public HorizontalProgressBarwithProgress(Context context) {
        this(context, null);
    }

    public HorizontalProgressBarwithProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalProgressBarwithProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        obtainStyleedAttrs(attrs);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(getPaddingLeft(), getHeight() / 2);//调整画布位置
        boolean noNeedReach = false;
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        //绘制reachBar
        String text = getProgress() + "%";
        int textWidht = (int) mPaint.measureText(text);
        float radio = getProgress() * 1.0f / getMax();//百分比
        float progressX = radio * RealWidth;
        float endX = progressX - mTextOffset / 2;//已有进度的结束位置
        if (progressX + textWidht > RealWidth) {//绘制到底了
            progressX = RealWidth - textWidht;
            noNeedReach = true;
            mPaint.setColor(mReachColor);
            mPaint.setStrokeWidth(mReachColor);
            canvas.drawLine(getPaddingLeft(), 0, progressX, 0, mPaint);
        }
        if (endX > 0 && !noNeedReach) {
            mPaint.setColor(mReachColor);
            mPaint.setStrokeWidth(mReachHeight);
            canvas.drawLine(getPaddingLeft(), 0, endX, 0, mPaint);
        }

        //绘制文本
        mPaint.setColor(mTextColor);
        int y = (int) (-(mPaint.descent() + mPaint.ascent()) / 2);
        canvas.drawText(text, progressX, y, mPaint);

        //绘制unReachBar
        if (!noNeedReach) {
            float start = progressX + textWidht + mTextOffset / 2;
            mPaint.setColor(mUnreachColor);
            mPaint.setStrokeWidth(mUnreachHeight);
            canvas.drawLine(start, 0, RealWidth, 0, mPaint);
        }
        canvas.restore();
    }

    @Override//测量宽高
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthVal = MeasureSpec.getSize(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        //确定宽度、高度
        setMeasuredDimension(widthVal, height);
        RealWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightVal = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {//精确值，有固定大小
            // result = heightVal;
            int textHeight = (int) (mPaint.descent() - mPaint.ascent());
            result = Math.max(heightVal, Math.abs(textHeight));
        } else {
            int textHeight = (int) (mPaint.descent() - mPaint.ascent());
            result = getPaddingTop() + getPaddingBottom() + Math.max(Math.max(mReachHeight, mUnreachHeight), Math.abs(textHeight));
            if (heightMode == MeasureSpec.AT_MOST) {//最大值
                result = Math.min(result, heightVal);
            }
        }
        return result;
    }

    //初始化自定义属性
    private void obtainStyleedAttrs(AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBarwithProgress);
        mTextSize = (int) ta.getDimension(R.styleable.HorizontalProgressBarwithProgress_progress_text_size, mTextSize);
        mTextOffset = (int) ta.getDimension(R.styleable.HorizontalProgressBarwithProgress_progress_text_offset, mTextOffset);
        mTextColor = ta.getColor(R.styleable.HorizontalProgressBarwithProgress_progress_text_color, mTextColor);
        mReachHeight = (int) ta.getDimension(R.styleable.HorizontalProgressBarwithProgress_progress_reach_height, mReachHeight);
        mReachColor = ta.getColor(R.styleable.HorizontalProgressBarwithProgress_progress_reach_color, mReachColor);
        mUnreachHeight = (int) ta.getDimension(R.styleable.HorizontalProgressBarwithProgress_progress_unreach_height, mUnreachHeight);
        mUnreachColor = ta.getColor(R.styleable.HorizontalProgressBarwithProgress_progress_unreach_color, mUnreachColor);
        ta.recycle();
        mPaint.setTextSize(mTextSize);
    }

    protected int dp2px(int dpValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getResources().getDisplayMetrics());
    }

    protected int sp2px(int spValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, getResources().getDisplayMetrics());
    }

    public int getTextOffset() {
        return mTextOffset;
    }

    public void setTextOffset(int textOffset) {
        mTextOffset = textOffset;
        invalidate();
    }

    public int getReachHeight() {
        return mReachHeight;
    }

    public void setReachHeight(int reachHeight) {
        mReachHeight = reachHeight;
        invalidate();
    }

    public int getReachColor() {
        return mReachColor;
    }

    public void setReachColor(int reachColor) {
        mReachColor = reachColor;
        invalidate();
    }

    public int getUnreachHeight() {
        return mUnreachHeight;
    }

    public void setUnreachHeight(int unreachHeight) {
        mUnreachHeight = unreachHeight;
        invalidate();
    }

    public int getUnreachColor() {
        return mUnreachColor;
    }

    public void setUnreachColor(int unreachColor) {
        mUnreachColor = unreachColor;
        invalidate();
    }

    public int getTextColor() {
        return mTextColor;
    }

    public void setTextColor(int textColor) {
        mTextColor = textColor;
        invalidate();
    }

    public int getTextSize() {
        return mTextSize;
    }

    public void setTextSize(int textSize) {
        mTextSize = textSize;
        invalidate();
    }
}
